package com.hui.auth.config.shiro;

import com.hui.auth.entity.AuthUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Assert;


/**
 * 安全工具封装
 *
 * @author 廖云辉
 */
public class SecurityUtil {

    public static String USER_KEY = "redisUser";

    /**
     * 获取当前session
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取当前用户
     */
    public static AuthUser currentUser() {
        Session session = SecurityUtils.getSubject().getSession();
        return (AuthUser) session.getAttribute(USER_KEY);
    }

    /**
     * 缓存用户
     */
    public static void cacheUser(AuthUser user) {
        Assert.isTrue(null != user);
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(USER_KEY, user);
    }
}
